package YottaTron;

import YottaTron.communication.CommunicationUDP;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.JFrame;

public class GameApplet extends Applet {


    public int serverPort = 1234;     // server details
    String host = "localhost";        //"85.70.59.167"; //"88.103.164.62"

    CommunicationUDP comm;

    ChatGui gui;

    public GameApplet() {
    }

    @Override
    public void init(){
        gui = new ChatGui();
        Runner runner = new Runner(true);
        comm = new CommunicationUDP(host, serverPort, gui, runner);

        runner.addComm(comm);
        /* thread v runneru bude odstartovan az po pripojeni k serveru a ziskani id
         * coz se stane v CommunicationUDP run() ID
         */
        //new Thread(runner).start();
       
        gui.addComm(comm);
        this.setLayout(new BorderLayout());
        this.add(runner, BorderLayout.NORTH);
        this.add(gui, BorderLayout.SOUTH);
        //comm.sendNick(gui.tfNick.getText());

    }

    @Override
    public void destroy() {
        comm.unsignFromServer();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final GameApplet applet = new GameApplet();

        JFrame frame = new JFrame("MultiChat Client");
        frame.add(applet);
        applet.init();
        applet.start();
        frame.pack();

        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                applet.destroy();
                System.exit(1);
            }
        });

        frame.setVisible(true);
    }
}
